<?php
/** Yz_SQL_Statement_Delete
 *
 *  @todo support multiple tables delete with table aliasing
 *  XXX        or just create another class MultiDelete ???
 */

class Yz_SQL_Statement_Delete
    extends Yz_SQL_DMLStatement
    implements Yz_SQL_Statement_Interface
{

    const CLAUSE_DELETE = 'DELETE';
    const CLAUSE_IGNORE = 'IGNORE';
    const CLAUSE_WHAT   = 'what';
    const CLAUSE_FROM   = 'FROM';
    const CLAUSE_TABLES = 'tables';
    const CLAUSE_WHERE  = 'WHERE';
    const CLAUSE_ORDER_BY = 'ORDER BY';
    const CLAUSE_LIMIT  = 'LIMIT';

    protected $clauses = array(
        self::CLAUSE_DELETE => '',
        self::CLAUSE_IGNORE => '',
        self::CLAUSE_FROM   => '',
        self::CLAUSE_TABLES => '',
        self::CLAUSE_WHERE  => '',
        self::CLAUSE_ORDER_BY => '',
        self::CLAUSE_LIMIT  => '',
    );


    /**
     *  @param string what
     *  @todo make "what" sensible
     */
    public function delete( $what=null )
    {
        $this->clauses[self::CLAUSE_DELETE] = self::CLAUSE_DELETE;
        return $this;
    }

    public function from( $table, $alias=null )
    {
        return $this->_table(self::CLAUSE_FROM, $table, $alias);
    }


    public function where( $what )
    {
        return $this->_where(self::CLAUSE_WHERE, $what);
    }

    public function orderBy( $what )
    {
        return $this->_orderBy(self::CLAUSE_ORDER_BY, $what);
    }

    public function limit( $limit )
    {
        $limit = Yz_Int::testNotNegative( $limit );
        return $this->_limit(0, $limit);
    }


    public function ignore( $ignore=true )
    {
        $this->clauses[self::CLAUSE_IGNORE] = $ignore ? self::CLAUSE_IGNORE : "";

        return $this;
    }


    /** isComplete() : is it possible to execute sql statement ?
     *
     *  @return string SQL
     */
    protected function isComplete()
    {
        debug();

        $clauses = $this->clauses;

        return ! ( "" === $clauses[self::CLAUSE_DELETE] || "" === $clauses[self::CLAUSE_FROM] );
    } // isComplete


} // class

